package mongo;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.MongoException;
import com.mongodb.client.MongoCursor;
import org.bson.conversions.Bson;

import java.util.List;


/**
 * @author : zGame
 * @version V1.0
 * @Project: mongo-java-demo
 * @Package mongo
 * @Description: mongodb 查询工具
 * @date Date : 2022年02月08日 11:43
 */
public class DBSearchTool extends DbUtil {
    
    
    public DBSearchTool(String dbName) {
        super(dbName);
    }
    
    /**
     * Bson Filters,Bson Projection,Bson Sorts 查找第一个
     *
     * @param collectionName
     * @param filter
     * @param projectionFields
     */
    public JSONObject searchFirst(String collectionName, Bson filter, Bson projectionFields, Bson sorts) {
        return getCollection(collectionName).find(filter).projection(projectionFields).sort(sorts).first();
    }
    
    /**
     * Bson Filters,Bson Projection,Bson Sorts 查找结果 返回游标
     *
     * @param collectionName
     * @param filter
     * @param projectionFields
     * @param sorts
     * @return
     */
    public MongoCursor search(String collectionName, Bson filter, Bson projectionFields, Bson sorts) {
        return getCollection(collectionName).find(filter).projection(projectionFields).sort(sorts).iterator();
    }
    
    /**
     * Bson Filters, Bson Updates
     *
     * @param collectionName
     * @param query
     * @param updates
     * @return
     */
    public boolean updateOne(String collectionName, Bson query, Bson updates) {
        //            Bson updates = Updates.combine(
        //                    Updates.set("runtime", 99),
        //                    Updates.addToSet("genres", "Sports"),
        //                    Updates.currentTimestamp("lastUpdated"));
        return getCollection(collectionName).updateOne(query, updates, updateOptions).getModifiedCount() == 1;
    }
    
    public boolean updateMany(String collectionName, Bson query, Bson updates) {
        return getCollection(collectionName).updateMany(query, updates, updateOptions).getModifiedCount() > 0;
    }
    
    /**
     * 批量操作  默认 遇到错误不会继续执行下一条
     *
     * @param collectionName
     * @param bulkList
     */
    public boolean bulkWrite(String collectionName, List bulkList) {
        //官网demo
        //        Arrays.asList(
        //                new InsertOneModel<>(new Document("name", "A Sample Movie")),
        //                new InsertOneModel<>(new Document("name", "Another Sample Movie")),
        //                new InsertOneModel<>(new Document("name", "Yet Another Sample Movie")),
        //                new UpdateOneModel<>(new Document("name", "A Sample Movie"),
        //                        new Document("$set", new Document("name", "An Old Sample Movie")),
        //                        new UpdateOptions().upsert(true)),
        //                new DeleteOneModel<>(new Document("name", "Yet Another Sample Movie")),
        //                new ReplaceOneModel<>(new Document("name", "Yet Another Sample Movie"),
        //                        new Document("name", "The Other Sample Movie").append("runtime",  "42"))
        //        )
        
        // new InsertOneModel<>(new JSONObject().put("name", "A Sample Movie"));
        try {
            getCollection(collectionName).bulkWrite(bulkList);
        } catch (MongoException me) {
            return false;
        }
        return true;
        
    }
    
}
